let connection = require("../dbutil/dbutil");
let myModule = require("./my-module");
let moment = require("moment");//引入日期格式类库
// 检测用户身份，账号密码是否正确
function checkInfo(username,password, callback) {
    let sql = "select uid from user where username = ? and password = ?";
    let sqlparams=[username,password]
    connection.query(sql,sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                id: -1,
                token: "null"
            }
            if (result.length > 0) {
                let token =  myModule.generateToken(result[0].uid)
                let sql = "UPDATE user SET token = '" + token + "' WHERE uid = '" + result[0].uid + "'"
                connection.query(sql, function (err) {
                    if (err) {
                        console.log(err);
                    }}) // 保存 token
                ret = {
                    code: 1001,
                    id: result[0].uid,
                    token: token
                }
            }
            callback(ret)
        }
    })
}
// 找回密码
function getPwd(username, tel, callback) {
    let sql = "select password from user where username = ? and tel = ?";
    let sqlparams=[username,tel]
    connection.query(sql,sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                pwd: "null"
            }
            if (result.length > 0) {
                ret = {
                    code: 1001,
                    pwd: result[0].password
                }
            }
            callback(ret)
        }
    })
}

// 进行用户的添加
function addUser(username, password, tel, callback) {
    let sql="select * from user where tel = ?";
    let sqlparams=[tel]
    connection.query(sql,sqlparams, function (err,   result) {
        if (err) {
            console.log(err)
        } else {
            if (result.length>0) {
                callback(2)
            } else {
                let sql="select * from user where username = '" + username + "'";
                connection.query(sql, function (err,   result) {
                    if (result.length>0) {
                        callback(3)
                    } else {
                        var addSql = 'INSERT INTO user(username, password, tel) VALUES(?, ?, ?)';
                        var addSqlParams = [username, password,tel];
                        connection.query(addSql,addSqlParams, function (err, result) {
                            if (err) {
                                console.log(err)
                            } else {
                                if(result.affectedRows==1){
                                    let time = moment().format('YYYY-MM-DD HH:mm:ss')
                                    let insql = "INSERT INTO `trade` (`uid`, `tmoney`, `type`, `ttime`, `remark`, `balance`) VALUES (?, ?, ?, ?, ?, ?)"
                                    let fsql = "select uid from user where username = ? and password = ?";
                                    let sqlparams=[username,password]
                                    connection.query(fsql,sqlparams, function (err, result) {
                                        if (err) {
                                            console.log(err)
                                        } else {
                                            let inSqlParams = [result[0].uid, 20000, "充值" , time, "系统赠送", 20000];
                                            connection.query("INSERT INTO `wallet` (`uid`, `wmoney`) VALUES ("+ result[0].uid +", 20000)", function (err) {
                                                if (err) {
                                                    console.log(err)
                                                } else {
                                                    connection.query(insql, inSqlParams, function (err) {
                                                        if (err) {
                                                            console.log(err)
                                                        } else {
                                                            callback(1)
                                                        }
                                                    })
                                                }
                                            })
                                        }
                                    })
                                }else{
                                    callback(-1)
                                }
                            }
                        })
                    }
                })
            }
        }
    })
}

exports.checkInfo = checkInfo;
exports.getPwd = getPwd;
exports.addUser = addUser;